import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router'
@Component({
  selector: 'app-signin',
  templateUrl: './signin.component.html',
  styleUrls: ['./signin.component.css']
})
export class SigninComponent implements OnInit {
  public err_msg:string = ''
  signinForm = {
    email:'',
    password:''
  }
  signin(){
    const formData = this.signinForm
    console.log('登录')
    this.http.post('http://localhost:3000/session',formData).toPromise().then((data:any) => {
      console.log(data)
      window.localStorage.setItem('auth_token',data.token)
      window.localStorage.setItem('user_info',JSON.stringify(data.user))
    this.router.navigate(['contact'])}).catch(e =>{
      console.log(e)
      if(e.status === 401){
        window.alert("登录失败")
        this.err_msg ="邮箱或密码错误"

      }
    })
  }
  constructor(
    private http:HttpClient,
    private router:Router
  ) {}

  ngOnInit(): void {
  }

}
